home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-2.iso / os2 / os2cl015.zip / pmdlg.cpp < prev    next >
C/C++ Source or Header  |  1995-11-24  |  12KB  |  502 lines

  1. /* 
  2.  
  3.  
  4.     pmdlg.cpp (emx+gcc) 
  5.  
  6.     1994,95 Giovanni Iachello
  7.     This is freeware software. You can use or modify it as you wish,
  8.     provided that the part of code that I wrote remains freeware.
  9.     Freeware means that the source code must be available on request 
  10.     to anyone.
  11.     You must also include this notice in all files derived from this
  12.     file.
  13.  
  14.  
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. #define INCL_WINMLE
  21. #include "pmwin.h"
  22. #include "pmdlg.h"
  23. #include "pmstdres.h"
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26.  
  27. MRESULT PMDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  28. {
  29.     PMDialog* win=NULL;
  30.     if (msg==WM_INITDLG) {
  31.         win=(PMDialog*)mp2;
  32.         if (win) { 
  33.             WinSetWindowPtr(hwnd,0,win);
  34.             win->hwnd=hwnd;                  // e naturalmente aggiorna hwnd ... ;)
  35.             win->hwndFrame=hwnd;
  36.             win->init();
  37.             win->initdlg();
  38.             return (MRESULT)TRUE;               // if focus changed
  39.         }
  40.     }
  41.  
  42.     win = (PMDialog*)WinQueryWindowPtr(hwnd, 0);
  43.     if (win) {
  44.         PMEvent event(msg,mp1,mp2,CHARMSG(&msg),MOUSEMSG(&msg));
  45.         return win->msgProc(event);
  46.     }
  47.     return WinDefDlgProc(hwnd,msg,mp1,mp2);
  48. }
  49.  
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52.  
  53. PMControl::PMControl(HWND parentDlg,void* iobj,int ilen,int iid) :
  54.     PMWin(WinQueryAnchorBlock(parentDlg))
  55. {
  56.     hwndFrame=parentDlg; obj=iobj; len=ilen; id=iid;
  57.     hwnd=WinWindowFromID(hwndFrame,id);
  58.     // in realta' questo e' solo accessorio perche' non serve al sistema
  59.     // inoltre nota che molti controlli (MLE...) non non hanno spazio
  60.     // per questo dato.
  61.     WinSetWindowPtr(hwnd,0,this); // okkey
  62. }
  63.  
  64. BOOL PMControl::destroyWin()
  65. {
  66.     return WinDestroyWindow(hwnd);
  67. }
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70.  
  71. PMButton::PMButton(HWND parentDlg,void* iobj,int ilen,int iid) :
  72.     PMControl(parentDlg,iobj,ilen,iid) 
  73. {
  74. }
  75.  
  76. void PMButton::get() 
  77. }
  78.  
  79. void PMButton::set() 
  80. }
  81.  
  82. /////////////////////////////////////////////////////////////////////////////
  83.  
  84. PMRadioButtonGroup::PMRadioButtonGroup(HWND parentDlg,void* iobj,int ilen,int iid) :
  85.     PMButton(parentDlg,iobj,ilen,iid) 
  86. {
  87. }
  88.  
  89. void PMRadioButtonGroup::get() 
  90.     int ret=queryIndex();
  91.     *((int*)obj)=ret;
  92. }
  93.  
  94. void PMRadioButtonGroup::set() 
  95.     HWND hwndctrl=WinWindowFromID(hwndFrame,id+(*(int*)obj)); // trova hwnd del controllo da attivare
  96.     WinSendMsg(hwndctrl,BM_SETCHECK,(MPARAM)1,(MPARAM)0);
  97. }
  98.  
  99. /////////////////////////////////////////////////////////////////////////////
  100.  
  101. PMCheckBox::PMCheckBox(HWND parentDlg,void* iobj,int ilen,int iid) :
  102.     PMButton(parentDlg,iobj,ilen,iid) 
  103. {
  104. }
  105.  
  106. void PMCheckBox::get() 
  107.     int ret=queryCheck();
  108.     *((int*)obj)=ret;
  109. }
  110.  
  111. void PMCheckBox::set() 
  112.     setCheck(*((int*)obj));
  113. }
  114.  
  115. /////////////////////////////////////////////////////////////////////////////
  116.  
  117. PMEntryField::PMEntryField(HWND parentDlg,void* iobj,int ilen,int iid) :
  118.     PMControl(parentDlg,iobj,ilen,iid) 
  119.     WinSendDlgItemMsg(hwndFrame,id,EM_SETTEXTLIMIT,MPFROMSHORT(len),0);
  120. }
  121.  
  122. void PMEntryField::get() 
  123.     WinQueryDlgItemText(hwndFrame,id,len,(PSZ)obj);
  124. }
  125.  
  126. void PMEntryField::set() 
  127.     WinSetDlgItemText(hwndFrame,id,(PSZ)obj);
  128. }
  129.  
  130. /////////////////////////////////////////////////////////////////////////////
  131.  
  132. PMMultiLineEdit::PMMultiLineEdit(HWND parentDlg,void* iobj,int ilen,int iid) :
  133.     PMControl(parentDlg,iobj,ilen,iid) 
  134.     WinSendDlgItemMsg(hwndFrame,id,MLM_SETTEXTLIMIT,MPFROMSHORT(len),0);
  135. }
  136.  
  137. void PMMultiLineEdit::get() 
  138.     WinQueryDlgItemText(hwndFrame,id,len,(PSZ)obj);
  139. }
  140.  
  141. void PMMultiLineEdit::set() 
  142.     WinSetDlgItemText(hwndFrame,id,(PSZ)obj);
  143. }
  144.  
  145. ////////////////////////////////////////////////////////////////////////////
  146.  
  147. PMListBox::PMListBox(HWND parentDlg,void* iobj,int ilen,int iid) :
  148.     PMControl(parentDlg,iobj,ilen,iid) 
  149. }
  150.  
  151. PMListBox::~PMListBox()
  152. {
  153. }
  154.  
  155. void PMListBox::get()
  156. {
  157.     ULONG ret=(ULONG)WinSendMsg(hwnd,LM_QUERYSELECTION,MPFROMLONG(LIT_FIRST),(MPARAM)NULL);
  158.     *((int*)obj)=ret;
  159. }
  160.  
  161. void PMListBox::set()
  162. {
  163.     WinSendMsg(hwnd,LM_SELECTITEM,MPFROMLONG(*((int*)obj)),(MPARAM)NULL);
  164. }
  165.  
  166. ////////////////////////////////////////////////////////////////////////////
  167.  
  168. PMValueSet::PMValueSet(HWND parentDlg,void* iobj,int ilen,int iid) :
  169.     PMControl(parentDlg,iobj,ilen,iid) 
  170. }
  171.  
  172. void PMValueSet::get()
  173. {
  174.     MRESULT ret=querySelectedItem();
  175.     *((MRESULT*)obj)=ret;
  176. }
  177.  
  178. void PMValueSet::set()
  179. {
  180.     selectItem(*(MPARAM*)obj);
  181. }
  182.  
  183. ////////////////////////////////////////////////////////////////////////////
  184.  
  185. PMSpinButton::PMSpinButton(HWND parentDlg,void* iobj,int ilen,int iid) :
  186.     PMControl(parentDlg,iobj,ilen,iid) 
  187. }
  188.  
  189. void PMSpinButton::get()
  190. {
  191.     queryValue((PVOID)obj,0,SPBQ_ALWAYSUPDATE);
  192. }
  193.  
  194. void PMSpinButton::set()
  195. {
  196.     setCurrentValue(*(LONG*)obj);
  197. }
  198.  
  199. ////////////////////////////////////////////////////////////////////////////
  200.  
  201. PMScrollBar::PMScrollBar(HWND parentDlg, void* iobj, int ilen, int iid) :
  202.     PMControl(parentDlg,iobj,ilen,iid)
  203. {
  204. }
  205.  
  206. PMScrollBar::PMScrollBar(PMWin* win, int* iobj, int iid)    :
  207.     PMControl(win->getHwndFrame(),iobj,4,iid)
  208. {
  209. }
  210.  
  211. void PMScrollBar::get()
  212. {
  213.     *(int*)obj=int(queryPos());
  214. }
  215.  
  216. void PMScrollBar::set()
  217. {
  218.     setPos(SHORT(*(int*)obj));
  219. }
  220.  
  221. PMVertScrollBar::PMVertScrollBar(PMWin* win, int* iobj) :
  222.     PMScrollBar(win,iobj,FID_VERTSCROLL)
  223. {
  224. }
  225.  
  226. PMHorizScrollBar::PMHorizScrollBar(PMWin* win, int* iobj) :
  227.     PMScrollBar(win,iobj,FID_HORZSCROLL)
  228. {
  229. }
  230.  
  231.  
  232. /////////////////////////////////////////////////////////////////////////////
  233.  
  234. PMDialog::PMDialog(HWND p,HWND o) : PMWin("",WinQueryAnchorBlock(o)) // chiama il costruttore complesso che riempie createargs
  235. {
  236.     createArgs->hwndParent=p;
  237.     createArgs->hwndOwner=o;
  238.     createArgs->hmod=NULLHANDLE;    
  239. }
  240.  
  241. PMDialog::~PMDialog()
  242. {
  243. }
  244.  
  245. void PMDialog::init()
  246. {
  247. }
  248.  
  249. BOOL PMDialog::createWin()
  250. {
  251.     return FALSE;
  252. }
  253.  
  254. BOOL PMDialog::destroyWin()
  255. {
  256.     return WinDismissDlg(hwnd,FALSE);
  257. }
  258.  
  259.  
  260. BOOL PMDialog::dispachEvent(PMEvent & event)
  261. {
  262.     switch (event.msg) {
  263.         case WM_CONTROL: 
  264.             return control(SHORT1FROMMP(event.mp1),SHORT2FROMMP(event.mp1));
  265.         case WM_INITDLG:
  266.             return initdlg();
  267.     }               
  268.     return PMWin::dispachEvent(event);
  269. }
  270.  
  271. BOOL PMDialog::control(SHORT id,SHORT ctrlmsg)
  272. {
  273.     return FALSE;
  274. }    
  275.  
  276. BOOL PMDialog::initdlg()
  277. {
  278.     return FALSE;
  279. }    
  280.  
  281. BOOL PMDialog::command(USHORT id,USHORT cmddev)
  282. {
  283.     return FALSE;
  284. }    
  285.  
  286. /////////////////////////////////////////////////////////////////////////////
  287.  
  288. PMModalDialog::PMModalDialog(HWND p,HWND o,ULONG i,PMControlMap *icm,void* iobj) : 
  289.     PMDialog(p,o)
  290. {
  291.     id=i;
  292.     cm=icm;
  293.     obj=iobj;
  294.     delctlmap=FALSE;
  295. }
  296.  
  297. PMControl* PMModalDialog::controlFromID(int id)
  298.     PMControl* ctl;
  299.     ctl=(PMControl*)WinQueryWindowPtr(WinWindowFromID(hwnd,id),0); 
  300.     if (!ctl) {
  301.         PMControlMap* pcm=cm;
  302.         for ( ; pcm->createfn!=NULL; pcm++ ) 
  303.             if (pcm->id == id) { ctl=pcm->ctrl;    break; }
  304.     }
  305.     assert(ctl);
  306.     return ctl;
  307. }
  308.  
  309. PMModalDialog::~PMModalDialog()
  310. {
  311.     if (delctlmap) {
  312.         PMControlMap* pcm=cm;
  313.         for ( ; pcm->createfn!=NULL; pcm++ ) {
  314.             delete pcm->ctrl; // elimina i controlli creati...
  315.         }
  316.         delctlmap=FALSE;
  317.     }
  318. }
  319.  
  320. void PMModalDialog::init()
  321. {
  322.     PMControlMap* pcm=cm;
  323.     for ( ; pcm->createfn!=NULL; pcm++ ) {
  324.         PMControl* ctl=pcm->createfn(hwnd,(char*)obj+pcm->offset,pcm->len,pcm->id);
  325.         assert(ctl);
  326.         ctl->set(); // imposta sullo schermo
  327.         pcm->ctrl=ctl; // salva anche nella mappa dei controlli
  328.     }
  329.     delctlmap=TRUE;
  330.     WinSetFocus(HWND_DESKTOP,WinWindowFromID(hwnd,pcm->id));
  331. }    
  332.  
  333. void PMModalDialog::updateControls()
  334. {
  335.     PMControlMap* pcm=cm;
  336.     for ( ; pcm->createfn!=NULL; pcm++ ) {
  337.         pcm->ctrl->set(); // imposta sullo schermo
  338.     }
  339. }
  340.  
  341. void PMModalDialog::updateObject()
  342. {
  343.     PMControlMap* pcm=cm;
  344.     for ( ; pcm->createfn!=NULL; pcm++ ) {
  345.         pcm->ctrl->get(); // imposta nella struttura
  346.     }
  347. }
  348.  
  349. BOOL PMModalDialog::createWin()
  350. {
  351.     oldproc=WinDefDlgProc;
  352.     ULONG ret=WinDlgBox(createArgs->hwndParent,createArgs->hwndOwner,PMDlgProc,createArgs->hmod,id,this);
  353.     return ret;
  354. }
  355.  
  356. BOOL PMModalDialog::command(USHORT id,USHORT cmddev)
  357. {
  358.     if (id==DID_OK) {
  359.         WinDismissDlg(hwnd,TRUE);
  360.         updateObject();        
  361.     }
  362.     if (id==DID_CANCEL) WinDismissDlg(hwnd,FALSE);
  363.     return TRUE;
  364. }    
  365.  
  366. BOOL PMModelessDialog::createWin()
  367. {
  368.     oldproc=WinDefDlgProc;
  369.     ULONG ret=WinLoadDlg(createArgs->hwndParent,createArgs->hwndOwner,PMDlgProc,createArgs->hmod,id,this);
  370.     return ret;
  371. }
  372.     
  373.  
  374. /////////////////////////////////////////////////////////////////////////////
  375.  
  376. PMFontDialog::PMFontDialog(HWND parent,HWND owner,FONTMETRICS *ifm,ULONG type,PSZ title,PSZ preview) : 
  377.     PMDialog(parent,owner) 
  378. {
  379.     fm=ifm;
  380.     memset((void*)&fontdlg, 0 , sizeof(FONTDLG));
  381.     fontdlg.flType= type;
  382.     fontdlg.pszTitle=title;
  383.     fontdlg.pszPreview=preview;
  384.     fontdlg.pszFamilyname=new CHAR[CCHMAXPATH]; // alloca zpazio per il nome della famiglia
  385. }
  386.  
  387. PMFontDialog::~PMFontDialog()
  388. {
  389.     delete fontdlg.pszFamilyname;
  390. }
  391.  
  392. BOOL PMFontDialog::createWin()
  393. {
  394.     fontdlg.cbSize=sizeof(FONTDLG);
  395.     fontdlg.hpsScreen = WinGetScreenPS(HWND_DESKTOP);
  396.     fontdlg.hpsPrinter= NULLHANDLE;
  397.     fontdlg.pfnDlgProc= PMFontDlgProc; 
  398.     fontdlg.ulUser= (ULONG)this;
  399.  
  400.     strcpy (fontdlg.pszFamilyname,fm->szFamilyname);
  401.     fontdlg.usFamilyBufLen=CCHMAXPATH;
  402.     fontdlg.fxPointSize= MAKEFIXED (fm->sNominalPointSize / 10 , 0);
  403.     fontdlg.fl= FNTS_CENTER | FNTS_HELPBUTTON | FNTS_NOSYNTHESIZEDFONTS | FNTS_RESETBUTTON;
  404.     fontdlg.clrFore=CLR_NEUTRAL;
  405.     fontdlg.clrBack=CLR_BACKGROUND;
  406.     fontdlg.usWeight=fm->usWeightClass;
  407.     fontdlg.usWidth=fm->usWidthClass;
  408.  
  409.     oldproc=WinDefFontDlgProc;
  410.  
  411.     WinFontDlg (createArgs->hwndParent,createArgs->hwndOwner, &fontdlg);
  412.     return fontdlg.lReturn;
  413. }
  414.  
  415. MRESULT PMFontDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  416. {
  417.     PFONTDLG fdlg;
  418.     PMFontDialog* win=NULL;  // il puntatore alla struttura fontdlg e' in WinPtr 0.
  419.  
  420.     fdlg = (PFONTDLG)WinQueryWindowPtr(hwnd, 0);
  421.     win = (PMFontDialog*) fdlg->ulUser;
  422.     if (win) {
  423.         if (msg==WM_INITDLG) {
  424.             win->hwnd=hwnd;                  // e naturalmente aggiorna hwnd ... ;)
  425.             win->hwndFrame=hwnd;
  426.             win->init();
  427.         }
  428.         PMEvent event(msg,mp1,mp2,CHARMSG(&msg),MOUSEMSG(&msg));
  429.         return win->msgProc(event);
  430.     }
  431.     return WinDefFontDlgProc(hwnd,msg,mp1,mp2);
  432. }
  433.  
  434.  
  435. /////////////////////////////////////////////////////////////////////////////
  436.  
  437. PMFileDialog::PMFileDialog(HWND parent,HWND owner,PSZ file,PSZ title,PSZ OKButton,ULONG flags) : 
  438.     PMDialog(parent,owner) 
  439. {
  440.     memset((void*)&filedlg, 0 , sizeof(FILEDLG));
  441.     filedlg.pszTitle=title;
  442.     filedlg.pszOKButton=OKButton;
  443.     filedlg.fl=flags;
  444.     strncpy(filedlg.szFullFile,file,CCHMAXPATH);
  445. }
  446.  
  447. PMFileDialog::~PMFileDialog()
  448. {
  449. }
  450.  
  451. BOOL PMFileDialog::createWin()
  452. {
  453.     filedlg.cbSize=sizeof(FILEDLG);
  454.     filedlg.pfnDlgProc= PMFileDlgProc; 
  455.     filedlg.ulUser= (ULONG)this;
  456.     filedlg.fl|= FDS_CENTER | FDS_PRELOAD_VOLINFO ;
  457.     filedlg.pszIDrive="C:";
  458.  
  459.     oldproc=WinDefFileDlgProc;
  460.     
  461.     WinFileDlg (createArgs->hwndParent,createArgs->hwndOwner, &filedlg);
  462.     return filedlg.lReturn;
  463. }
  464.  
  465. MRESULT PMFileDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  466. {
  467.     PFILEDLG fdlg;
  468.     PMFileDialog* win=NULL;  // il puntatore alla struttura fontdlg e' in WinPtr 0.
  469.  
  470.     fdlg = (PFILEDLG)WinQueryWindowPtr(hwnd, 0);
  471.     win = (PMFileDialog*) fdlg->ulUser;
  472.     if (win) {
  473.         if (msg==WM_INITDLG) {
  474.             win->hwnd=hwnd;                  // e naturalmente aggiorna hwnd ... ;)
  475.             win->hwndFrame=hwnd;
  476.             win->init();
  477.         }
  478.         PMEvent event(msg,mp1,mp2,CHARMSG(&msg),MOUSEMSG(&msg));
  479.         return win->msgProc(event);
  480.     }
  481.     return WinDefFileDlgProc(hwnd,msg,mp1,mp2);
  482. }
  483.  
  484.  
  485.  
  486.